﻿using System;
using System.Configuration;

namespace Eflong.Config
{
    public class KWBridgeConfig
    {
        private static KWBridgeConfig instance = null;

        public static KWBridgeConfig Instance
        {
            get
            {
                if ( instance == null )
                {
                    instance = new KWBridgeConfig( );
                }
                return instance;
            }
        }

        private KWBridgeConfig( )
        {
            IsSingleKJSystem = true;

            string s;
            s = ConfigurationManager.AppSettings[ "联网双系统" ];
            if ( s != null && ( s.ToLower( ) == "1" || s.ToLower( ) == "是" ) )
            {
                IsSingleKJSystem = false;
            }

            KJSysParas = new KWSysParas( );

            ReadConfig( );
        }

        /// <summary>
        /// 联网矿Id
        /// </summary>
        public uint Id
        {
            get;
            set;
        }

        /// <summary>
        /// 联网矿名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 联网服务器IP地址
        /// </summary>
        public string IPAddr { get; set; }

        /// <summary>
        /// Opc Server Name
        /// </summary>
        public string OpcServerName { get; set; }

        public string OpcServerHostName { get; set; }

        /// <summary>
        /// 读取实时数据文件间隔
        /// </summary>
        public uint Interval { get; set; }

        /// <summary>
        /// 网络中断间隔
        /// </summary>
        public uint BreakInterval { get; set; }

        /// <summary>
        /// 联网截止时间
        /// </summary>
        public string EndDateString { get; set; }

        public DateTime EndTime { get; set; }

        /// <summary>
        /// 联网授权字符串
        /// </summary>
        public string EncryString { get; set; }

        /// <summary>
        /// 标准化报警值
        /// </summary>
        public bool IsStartandAlarmLimit = true;

        /// <summary>
        /// 联网文件备份目录
        /// </summary>
        public string BackupDir = @"d:\databackup";

        public string BackupDir2 = @"d:\databackup2";

        /// <summary>
        /// 是否单一系统
        /// </summary>
        public bool IsSingleKJSystem { get; set; }

        /// <summary>
        /// 联网系统1测点名后缀
        /// </summary>
        public string Postfix { get; set; }

        /// <summary>
        /// 联网系统2测点名后缀
        /// </summary>
        public string Postfix2 { get; set; }

        /// <summary>
        /// 系统1 参数
        /// </summary>
        public KWSysParas KJSysParas { get; set; }

        /// <summary>
        /// 系统2 参数
        /// </summary>
        public KWSysParas KJSysParas2 { get; set; }

        /// <summary>
        /// 文本文件的类型
        ///  默认、UTF-8、ASCII
        /// </summary>
        public string KjFileType { get; set; }

        /// <summary>
        /// 是否是联网系统扩展
        /// 扩展测点定义名称、ID、矿名等字段数增加
        /// </summary>
        public bool IsKWSystemEx { get; set; }

        /// <summary>
        /// 是否采用联网标准协议
        /// </summary>
        public bool IsKWSystemNormal { get; set; }

        /// <summary>
        /// 限值设置配置文件
        /// </summary>
        public string LimitSetConfig { get; set; }

        /// <summary>
        /// 数据过滤配置文件
        /// </summary>
        public string DataFilterConfig { get; set; }

        /// <summary>
        /// 自定义限值
        /// </summary>
        public bool IsUseLimitSet { get; set; }

        /// <summary>
        /// 从应用程序配置文件读取联网参数
        /// </summary>
        public void ReadConfig( )
        {
            if ( !IsSingleKJSystem )
            {
                string strPostfix1 = ConfigurationManager.AppSettings[ "联网系统1测点名后缀" ];
                string strPostfix2 = ConfigurationManager.AppSettings[ "联网系统2测点名后缀" ];
                if ( strPostfix1 == null )
                {
                    strPostfix1 = string.Empty;
                }
                if ( strPostfix2 == null )
                {
                    strPostfix2 = string.Empty;
                }
                Postfix = strPostfix1;

                Postfix2 = strPostfix2;
            }
            else
            {
                Postfix = string.Empty;
                Postfix2 = string.Empty;
            }
            string s = string.Empty;
            Id = 1;
            var id = ConfigurationManager.AppSettings[ "联网系统编号" ];
            if ( id != null )
            {
                uint d = 0;
                if ( uint.TryParse( id , out d ) )
                {
                    Id = d;
                }
            }

            var name = ConfigurationManager.AppSettings[ "联网单位名称" ];
            if ( name != null )
            {
                Name = name;
            }
            else
            {
                Name = "测试矿";
            }

            s = ConfigurationManager.AppSettings[ "联网服务器IP" ];
            if ( s != null )
            {
                IPAddr = s;
            }
            else
            {
                IPAddr = "127.0.0.1";
            }

            OpcServerName = string.Empty;
            s = ConfigurationManager.AppSettings[ "OpcServerName" ];
            if ( s != null )
            {
                OpcServerName = s;
            }
            else
            {
                OpcServerName = string.Empty;
            }

            s = ConfigurationManager.AppSettings[ "OpcServer机器名" ];
            if ( s != null )
            {
                OpcServerHostName = s;
            }
            else
            {
                OpcServerHostName = string.Empty;
            }

            Interval = 10;

            var interval = ConfigurationManager.AppSettings[ "联网系统更新间隔" ];
            if ( interval != null )
            {
                uint d = 0;
                if ( uint.TryParse( interval , out d ) )
                {
                    Interval = d < 5 ? 5 : d;
                }
            }

            BreakInterval = 3;

            var breakInterval = ConfigurationManager.AppSettings[ "联网系统中断间隔" ];
            if ( breakInterval != null )
            {
                uint d = 0;
                if ( uint.TryParse( breakInterval , out d ) )
                {
                    BreakInterval = d;
                }
            }

            s = ConfigurationManager.AppSettings[ "联网到期日期" ];
            if ( s != null )
            {
                EndDateString = s;

                DateTime t = DateTime.Now;

                if ( DateTime.TryParse( EndDateString , out t ) )
                {
                    EndTime = t;
                }
                EndTime = t.AddDays( 1 );
            }
            else
            {
                EndDateString = DateTime.Now.ToString( "yyyy-mm-dd" );
                EndTime = DateTime.Now.AddHours( 2 );
            }

            s = ConfigurationManager.AppSettings[ "联网授权系列号" ];
            EncryString = s ?? "试用";

            s = ConfigurationManager.AppSettings[ "联网文本文件类型" ];
            KjFileType = s ?? "默认";

            // 标准化报警值
            s = ConfigurationManager.AppSettings[ "标准化报警值" ];

            if ( s != null )
            {
                bool.TryParse( s , out IsStartandAlarmLimit );
            }

            // 联网文件备份目录
            s = ConfigurationManager.AppSettings[ "联网文件备份目录" ];
            BackupDir = s ?? @"d:\databackup";

            KJSysParas.GetKJSysParas( ConfigurationManager.AppSettings );

            if ( !IsSingleKJSystem )
            {
                KJSysParas2 = new KWSysParas( );

                KJSysParas2.GetKJSysParas( ConfigurationManager.AppSettings );
            }

            bool bEx = false;
            s = ConfigurationManager.AppSettings[ "联网系统扩展" ];
            if ( s != null )
            {
                bool.TryParse( s , out bEx );
            }
            IsKWSystemEx = bEx;

            bool bNormal = false;
            s = ConfigurationManager.AppSettings[ "联网系统标准协议" ];
            if ( s != null )
            {
                bool.TryParse( s , out bNormal );
            }
            IsKWSystemNormal = bNormal;

            s = ConfigurationManager.AppSettings[ "限值配置" ];
            LimitSetConfig = s ?? string.Empty;

            s = ConfigurationManager.AppSettings[ "过滤配置" ];
            DataFilterConfig = s ?? string.Empty;

            bool IsLimitSet = false;
            s = ConfigurationManager.AppSettings[ "自定义限值" ];
            if ( s != null )
            {
                bool.TryParse( s , out IsLimitSet );
            }
            IsUseLimitSet = IsLimitSet;
        }

        /// <summary>
        /// 保存联网的参数到程序配置文件
        /// </summary>
        public void WriteConfig( )
        {
            Configuration cfg = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None );
            try
            {
                cfg.AppSettings.Settings[ "联网系统编号" ].Value = Id.ToString( );
            }
            catch ( Exception )
            {
                cfg.AppSettings.Settings.Add( "联网系统编号" , Id.ToString( ) );
            }
            try
            {
                cfg.AppSettings.Settings[ "联网单位名称" ].Value = Name;
            }
            catch ( Exception )
            {
                cfg.AppSettings.Settings.Add( "联网单位名称" , Name );
            }

            try
            {
                cfg.AppSettings.Settings[ "联网服务器IP" ].Value = IPAddr;
            }
            catch ( Exception )
            {
                cfg.AppSettings.Settings.Add( "联网服务器IP" , IPAddr );
            }

            try
            {
                cfg.AppSettings.Settings[ "联网授权系列号" ].Value = EncryString;
            }
            catch ( Exception )
            {
                cfg.AppSettings.Settings.Add( "联网授权系列号" , EncryString );
            }

            try
            {
                cfg.AppSettings.Settings[ "联网截止日期" ].Value = EndDateString;
            }
            catch ( Exception )
            {
                cfg.AppSettings.Settings.Add( "联网截止日期" , EndDateString );
            }
            KJSysParas.SetKJSysParas( cfg.AppSettings.Settings );

            if ( !IsSingleKJSystem )
            {
                KJSysParas2.SetKJSysParas( cfg.AppSettings.Settings );
            }

            cfg.Save( );
        }

        private static bool isDayAlarm = false;

        private static bool isHourAlarm = false;

        public bool IsHourAlarm( DateTime time , TimeSpan ts )
        {
            // 1天之内到期时，每小时开始报警
            if ( ts.TotalHours <= 24 && time.Minute == 0 && time.Second < 10 )
            {
                if ( !isHourAlarm )
                {
                    isHourAlarm = true;
                    return true;
                }
                else
                {
                    return false;
                }
            }
            isHourAlarm = false;
            return false;
        }

        public bool IsDayAlarm( DateTime time , TimeSpan ts )
        {
            // 7天之内到期时， 每天早上 7点 30分，开始报警
            if ( ts.TotalDays < 7 && ts.TotalDays >= 1 && time.Hour == 7 && time.Minute == 30 && time.Second < 10 )
            {
                if ( !isDayAlarm )
                {
                    isDayAlarm = true;
                    return true;
                }
                else
                {
                    return false;
                }
            }
            isDayAlarm = false;
            return false;
        }
    }
}